package View

import ControllerView.ControllerView
import com.trolltech.qt.gui._
import com.trolltech.qt.core._

/**
 * Created by IntelliJ IDEA.
 * User: danymorard
 * Date: 13.04.11
 * Time: 16:22
 * To change this template use File | Settings | File Templates.
 */

class WelcomeWindow (controller:ControllerView, parent:QWidget)  extends View(controller, parent) {
    /**
   * Size of the window
   */
  protected val WINDOW_SIZE = new QSize (439,456)

  /**
   * Size of the big button (button new project and open project)
   */
  protected val BIG_BUTTON_SIZE = new QSize (310,70)

  /**
   * Size of the icon for button
   */
  protected val BUTTON_SIZE_ICON = new QSize (310,70)


  /**
   * Position IN Y of the first button
   */
  protected val FIRST_BIG_BUTTON_Y = 200

  /**
   * Margin between big button
   */
  protected val MARGIN_BIG_BUTTON = 10


  /**
   * Name that is used in style sheet for the big button
   */
  protected val CSS_BIG_BUTTON = "buttonNewOpenProject"

  /**
   * Image for the background of the window
   */
  protected var backgroundPicture = new QLabel(this)

  /**
   * Version of SPDM display
   */

  private val SDPM_VERSION = "VERSION 0.9"

  /**
   * displayed program version
   */
  private var version = new QLabel(this)

  /**
   * button to close window
   */
//  protected var buttonExit = new QPushButton(this)

  /**
   * button to open a new project
   */
 // protected var buttonOkay = new QPushButton(this)

  /**
   * button to create a new project
   */
  protected var buttonNewProject = new QPushButton(this)

  /**
   * Button to open the help window
   */
  protected var buttonHelp = new QPushButton(this)

  /**
   * button to open the tools window
   */
  protected var buttonTools = new QPushButton(this)


  // Initialise the window
  this.initView


  /**
   * Initialise the view
   */
  protected def initView  {

    // Define the size of the window
    this.setFixedSize(WINDOW_SIZE)

    //set window in top
    this.setWindowState(Qt.WindowState.WindowActive)

    // set the background image of the window
    backgroundPicture.setPixmap(new QPixmap(PATH_PICTURE + "welcomeView.png"))
    backgroundPicture.setGeometry(0, 0, WINDOW_SIZE.width, WINDOW_SIZE.height)

    // display version app
    version.setText(SDPM_VERSION)
    version.setFont(new QFont("Calibri",17))
    version.adjustSize
    version.setGeometry((WINDOW_SIZE.width - version.size.width)/2+4,159,version.size.width, version.size.height)



    // Center the window in the middle of the screen
 /*   val screenWidget = QApplication.desktop
    this.move(screenWidget.width / 2 - WINDOW_SIZE.width / 2,
              screenWidget.height / 2 - WINDOW_SIZE.height / 2)
   */

    // set the title and the icon on the window
    this.setWindowIcon(new QIcon(PATH_PICTURE + "android.png"))
    this.setWindowTitle("ScalaDroid Project Manager")



    // add the different button to the window
    this.addButton

  }



  /**
   * Add the buttons to the window and connect slot
   */
  private def addButton {

 /*   // Configure the cancel button
    buttonExit.setGeometry(20, 465, SMALL_BUTTON_SIZE.width, SMALL_BUTTON_SIZE.height)
    buttonExit.setText("Exit")
    buttonExit.setObjectName(CSS_SMALL_BUTTON)
    buttonExit.clicked.connect(controller, "exitButtonClicked()")
   */
 /*
    // configure the okay button
    buttonOkay.setGeometry(575, 465, SMALL_BUTTON_SIZE.width, SMALL_BUTTON_SIZE.height)
    buttonOkay.setText("OK")
    buttonOkay.setObjectName(CSS_SMALL_BUTTON)
    buttonOkay.setEnabled(false)
    buttonOkay.clicked.connect(controller, "okButtonClicked()")
   */



    // configure the big button new project
    buttonNewProject.setIcon(new QIcon(PATH_PICTURE + "buttonNewProject.png"))
    buttonNewProject.setIconSize(BUTTON_SIZE_ICON)
    buttonNewProject.setGeometry(WINDOW_SIZE.width / 2 - BUTTON_SIZE_ICON.width / 2, FIRST_BIG_BUTTON_Y, BIG_BUTTON_SIZE.width, BIG_BUTTON_SIZE.height)
    buttonNewProject.setObjectName(CSS_BIG_BUTTON)
    buttonNewProject.clicked.connect(controller, "newProjectButtonClicked()")

    // configure the big button tools
    buttonTools.setIcon(new QIcon(PATH_PICTURE + "buttonTools.png"))
    buttonTools.setIconSize(BUTTON_SIZE_ICON)
    buttonTools.setGeometry(WINDOW_SIZE.width / 2 - BUTTON_SIZE_ICON.width / 2, FIRST_BIG_BUTTON_Y + BUTTON_SIZE_ICON.height + MARGIN_BIG_BUTTON, BIG_BUTTON_SIZE.width, BIG_BUTTON_SIZE.height)
    buttonTools.setObjectName(CSS_BIG_BUTTON)
    buttonTools.clicked.connect(controller, "toolsButtonClicked()")

    // configure the big button tools
    buttonHelp.setIcon(new QIcon(PATH_PICTURE + "buttonHelp.png"))
    buttonHelp.setIconSize(BUTTON_SIZE_ICON)
    buttonHelp.setGeometry(WINDOW_SIZE.width / 2 - BUTTON_SIZE_ICON.width / 2, FIRST_BIG_BUTTON_Y + 2 * BUTTON_SIZE_ICON.height + 2 * MARGIN_BIG_BUTTON, BIG_BUTTON_SIZE.width, BIG_BUTTON_SIZE.height)
    buttonHelp.setObjectName(CSS_BIG_BUTTON)
    buttonHelp.clicked.connect(controller, "helpButtonClicked()")
  }


   /**
   * Create a dialog view for browsing the path
   */
  def createBrowsingView {
    var path = QFileDialog.getExistingDirectory(new QWidget, new String("Open Directory"))

    if(path.length != 0){
      //ensure that opened directory is an Android project with an Ant toolchain
      if(!(new java.io.File(path + "/AndroidManifest.xml").exists() || new java.io.File(path + "/build.xml").exists())) {
        QMessageBox.critical(null, tr("ScalaDroid"),
          tr("This directory doesn't contain\n"+
            "a valid Android Project."), new QMessageBox.StandardButtons(QMessageBox.StandardButton.Close));

      }
      else{
        // ouvrir le projet contenu dans la var path
        QMessageBox.question(null, tr("ScalaDroid"),
          tr("Would you like to import\n"+
            "your projet into SDPM?"), new QMessageBox.StandardButtons(QMessageBox.StandardButton.Yes, QMessageBox.StandardButton.No));
      }
    }
  }
}